﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using Microsoft.Win32;

namespace information
{
    public partial class Encrypt : Form
    {
        public Encrypt()
        {
            InitializeComponent();
        }

        //获取CPU序列号
        public string GetCpu()
        {
            string strcpu = null;
            ManagementClass myCpu = new ManagementClass("win32_Processor");
            ManagementObjectCollection myCpuConnection = myCpu.GetInstances();   //GetInstances方法的返回值为ManagementObjectCollection对象所以不用new
            foreach(ManagementObject myObject in myCpuConnection)
            {
                strcpu = myObject.Properties["Processorid"].Value.ToString();
                break;
            }
            return strcpu;
            
        }


        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = GetCpu();
        }

        private void Encrypt_Load(object sender, EventArgs e)
        {
            label2.Text = "";
            label4.Text = "";
            label6.Text = "";
        }

        //获取硬盘ID
        public string GetDiskVolumeSerialNumber()
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"d:\"");
            disk.Get();
            return disk.GetPropertyValue("VolumeSerialNumber").ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label4.Text = GetDiskVolumeSerialNumber();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            label6.Text = GetCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号
            string[] strid = new string[24];
            for (int i = 0; i < 24; i++)//把字符赋给数组
            {
                strid[i] = label6.Text.Substring(i, 1);
            }
            label6.Text = "";
            Random rdid = new Random();
            for (int i = 0; i < 24; i++)//从数组随机抽取24个字符组成新的字符生成机器码
            {
                label6.Text += strid[rdid.Next(0, 24)];
            }
        }
    }
}
